package com.tywj.panda.customer.common.api

import com.tywj.panda.customer.common.tool.AppEnvTool
import com.tywj.panda.customer.common.tool.AuthorityTool
import com.tywj.panda.customer.common.tool.HttpTool
import com.tywj.lib.net.retrofit.request.PostRequest
import java.io.File

object CommonApi {

    /**********不同端口****************/

    //开发接口 （内网）
    private const val DEV_URL = "http://192.168.2.91:8080/esportingplus/v1/api/"
//    private const val DEV_URL = "https://commute.panda.cd917.com/esportingplus/v1/api/"

    //测试环境(预发布 外网)
    private const val TEST_URL = "https://commute.panda.cd917.com/esportingplus/v1/api/"

    //正式环境（外网）
    private const val RELEASE_URL = "https://commute.panda.cd917.com/esportingplus/v1/api/"

    var BASE_URL = getUrlByEnv(DEV_URL, TEST_URL, RELEASE_URL)

    //user
    val API_USER_URL = BASE_URL + "user/"

    //mgt
    val API_FNC_URL = BASE_URL + "mgt/"

    //route
    val API_ROUTE_URL = BASE_URL + "route/"

    //ticket
    val API_TICKET_URL = BASE_URL + "ticket/"


    //H5
    // 开发环境
    private const val H5_DEV_URL = "https://protocol.commute.panda.cd917.com/#/"

    // 测试环境
    private const val H5_TEST_URL = "https://protocol.commute.panda.cd917.com/#/"

    //正式环境
    private const val H5_RELEASE_URL = "https://protocol.commute.panda.cd917.com/#/"


    val H5_URL = getUrlByEnv(H5_DEV_URL, H5_TEST_URL, H5_RELEASE_URL)

    /**************************/
    // api开发地址 BASE_URL
    private val API_DEV_URL = API_USER_URL

    // api测试地址
    private val API_TEST_URL = API_USER_URL

    // api正式地址
    private val API_RELEASE_URL = API_USER_URL

    // api地址(BaseUrl)由于baseUrl的接口有多个，此处默认第一个，其他不同端口的直接用全url实现
    val API_URL = getUrlByEnv(API_DEV_URL, API_TEST_URL, API_RELEASE_URL)


    /**
     * 上传文件(图片)地址
     */
    private const val UPLOAD_DEV_URL = "http://hytdev/api/v1/"
    private const val UPLOAD_TEST_URL = "http://hyt-res/api/v1/"
    private const val UPLOAD_RELEASE_URL = "https://res/api/v1/"

    // 上传文件地址
    private val uploadUrl = getUrlByEnv(UPLOAD_DEV_URL, UPLOAD_TEST_URL, UPLOAD_RELEASE_URL)

    // 上传文件
    private val UPLOAD_FILE = uploadUrl + "upload/"

    // 更新token
    private const val UPDATE_TOKEN = "user/login"


    private fun getUrlByEnv(
        devUrl: String,
        testUrl: String,
        releaseUrl: String
    ): String {
        return when {
            AppEnvTool.isDev -> devUrl
            AppEnvTool.isTest -> testUrl
            else -> releaseUrl
        }
    }

    /**
     * 上传文件
     * @param type 文件类型
     * @param file 待上传的文件
     */
    fun uploadFile(type: String, file: File): PostRequest {
        return HttpTool.post("$UPLOAD_FILE?file_type=$type", isJson = false)
            .param("file", file)
    }

    /**
     * 刷新token
     */
    fun updateToken(): PostRequest {
        return HttpTool.post(UPDATE_TOKEN, false)
            .param("uid", AuthorityTool.uid)
            .param("token", AuthorityTool.token)
            .connectTimeoutMillis(5000L)
            .readTimeoutMillis(5000L)
            .writeTimeoutMillis(5000L)
    }
}